
#import "DDResetPWDViewController.h"

@interface DDResetPWDViewController ()<JTWTextFieldDelegate>
@property (nonatomic, strong) DDTextField *accountTF;
@property (nonatomic, strong) DDTextField *passTF;
@property (nonatomic, strong) DDTextField *phoneTF;
@property (nonatomic, strong) DDTextField *veriyTF;
@property (nonatomic, strong) DDButton *enterBtn;
@property (nonatomic, copy) NSString *verifycode;

@end

@implementation DDResetPWDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (DDTextField *)accountTF
{
    if (!_accountTF)
    {
        _accountTF = [[DDTextField alloc] init];
        _accountTF.type = JTWTextFieldTypeAccountNormal;
        _accountTF.returnKeyType = UIReturnKeyNext;
        _accountTF.customDelegate = self;
        [self.ddview addSubview:_accountTF];
        _accountTF.c_left.equal(self.ddview).with.coffset(GAP);
        _accountTF.c_right.equal(self.ddview).with.coffset(-GAP);
        _accountTF.c_height.equal(TEXTFILDHEIGHT);
        //        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        //            _accountTF.c_bottom.equal(self.veriyTF.c_top).with.coffset(-GAP);
        //        else
        _accountTF.c_top.equal(self.ddview.bar.c_bottom).with.coffset(GAP);
    }
    return _accountTF;
}
- (DDTextField *)passTF
{
    if (!_passTF)
    {
        _passTF = [[DDTextField alloc] init];
        _passTF.type = JTWTextFieldTypePassword;
        _passTF.returnKeyType = UIReturnKeyNext;
        _passTF.customDelegate = self;
        [self.ddview addSubview:_passTF];
        _passTF.c_left.equal(self.ddview).with.coffset(GAP);
        _passTF.c_right.equal(self.ddview).with.coffset(-GAP);
        _passTF.c_height.equal(TEXTFILDHEIGHT);
        _passTF.c_top.equal(self.accountTF.c_bottom).with.coffset(GAP);
    }
    return _passTF;
}
- (DDTextField *)phoneTF
{
    if (!_phoneTF)
    {
        _phoneTF = [[DDTextField alloc] init];
        _phoneTF.type = JTWTextFieldTypePhoneNumber;
        _phoneTF.returnKeyType = UIReturnKeyNext;
        _phoneTF.customDelegate = self;
        [self.ddview addSubview:_phoneTF];
        _phoneTF.c_left.equal(self.ddview).with.coffset(GAP);
        _phoneTF.c_right.equal(self.ddview).with.coffset(-GAP);
        _phoneTF.c_height.equal(TEXTFILDHEIGHT);
        _phoneTF.c_top.equal(self.passTF.c_bottom).with.coffset(GAP);
    }
    return _phoneTF;
}
- (DDTextField *)veriyTF
{
    if (!_veriyTF)
    {
        _veriyTF = [[DDTextField alloc] init];
        _veriyTF.type = JTWTextFieldTypeVerify;
        _veriyTF.returnKeyType = UIReturnKeyNext;
        _veriyTF.customDelegate = self;
        [self.ddview addSubview:_veriyTF];
        _veriyTF.c_left.equal(self.ddview).with.coffset(GAP);
        _veriyTF.c_right.equal(self.ddview).with.coffset(-GAP);
        _veriyTF.c_height.equal(TEXTFILDHEIGHT);
        _veriyTF.c_top.equal(self.phoneTF.c_bottom).with.coffset(GAP);
    }
    return _veriyTF;
}

- (DDButton *)enterBtn
{
    if (!_enterBtn)
    {
        _enterBtn = [[DDButton alloc] init];
        _enterBtn.title = @"重置密码";
        _enterBtn.backgroundColor = helpermanger().getPara().color;
        _enterBtn.layer.cornerRadius = GAP / 2.0;
        [_enterBtn addTarget:self action:@selector(yjd_actionForResetPWD) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_enterBtn];
        _enterBtn.c_left.equal(self.ddview).with.coffset(GAP);
        _enterBtn.c_right.equal(self.ddview).with.coffset(-GAP);
        _enterBtn.c_height.equal(BUTTONHEIGHT);
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            _enterBtn.c_top.equal(self.ddview.c_centerY).with.coffset(GAP * 2);
        else
            _enterBtn.c_top.equal(self.veriyTF.c_bottom).with.coffset(GAP);
    }
    return _enterBtn;
}
- (void)yjd_actionForResetPWD
{
    if (![self.accountTF.text isNumberOrAlphabet])
        return [CCAlert showMessage:@"账号仅限于字母和数字的组合" hidden:nil];
    else if (![self.passTF.text isNumberOrAlphabet])
        return [CCAlert showMessage:@"密码仅限于字母和数字的组合" hidden:nil];
    else if(![self.phoneTF.text isMobile])
        return [CCAlert showMessage:@"手机号码有误" hidden:nil];
    else if (![self.veriyTF.text isEqualToString:self.verifycode])
        return [CCAlert showMessage:@"验证码有误" hidden:nil];
    
    if ([self.veriyTF.text isEqualToString:self.verifycode] && [self.phoneTF.text isMobile])
    {
        [CCHud startHud];
        [self.request yjd_resetPWDRequest:@{@"account":self.accountTF.text,@"pass":self.passTF.text,@"phone":self.phoneTF.text}];
    }
}
#pragma -mark JTWTextFieldDelegate method
- (BOOL)yjd_actionForVerifyCode
{
    if ([self.phoneTF.text isMobile])
    {
        [CCHud startHud];
        [self.request yjd_verifyCodeRequest:@{@"phone":self.phoneTF.text,@"account":self.accountTF.text}];
        return YES;
    }
    else
    {
        [CCAlert showMessage:@"手机号码有误" hidden:nil];
        return NO;
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([textField isEqual:self.accountTF] && ![string isNumberOrAlphabet] && ![string isEqualToString:@""])
        return NO;
    else if ([textField isEqual:self.passTF] && ![string isNumberOrAlphabet] && ![string isEqualToString:@""])
        return NO;
    else if ([textField isEqual:self.phoneTF] && [textField.text length] > 11 && ![string isNumber] && ![string isEqualToString:@""])
        return NO;
    else if ([textField isEqual:self.veriyTF] && [textField.text length] > 5 && ![string isNumber] && ![string isEqualToString:@""])
        return NO;
    else
        return YES;
}
#pragma -mark JTWRequestDelegate method
- (void)yjd_didRequestWithReceiveData:(NSDictionary *)dict error:(NSError *)error
{
    [CCHud stopHud];
    if(error)
    {
        if ((error.code != -999 || ![error.localizedDescription isEqualToString:@"cancelled"]))
            [CCAlert showMessage:[NSString stringWithFormat:@"%@",error.localizedDescription] hidden:nil];
        return;
    }
    if (!dict)
        return;
    
    int code = -1;
    code = [[dict valueForKey:@"code"] intValue];
    NSString *info = [NSString stringWithFormat:@"%@",[dict valueForKey:@"info"]];
    if (code == 0)
    {
        if (self.request.type == JTWRequestTypeVerifyCode)
        {
            self.verifycode = [NSString stringWithFormat:@"%@",[dict valueForKey:@"verifycode"]];
        }
        else if (self.request.type == JTWRequestTypeResetPWD)
        {
            helpermanger().insertAccountDict(@{@"name":self.accountTF.text,@"password":self.passTF.text});
            [CCAlert showMessage:info hidden:^{
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
        }
    }
    else
    {
        [CCAlert showMessage:info title:@"错误" enter:nil];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.accountTF.placeholder = @"请输入账号";
    self.passTF.placeholder = @"请设置新密码";
    self.phoneTF.placeholder = @"请输入已绑定的手机号";
    self.veriyTF.placeholder = @"请输入验证码";
    self.enterBtn.hidden = NO;
}
@end
